<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>点击变色</title>
</head>
<body>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <div class="box" style="background-color: greenyellow;"></div>
    <script>
        var boxs = document.getElementsByClassName('box');
        for(var i = 0;i<boxs.length;i++){
            boxs[i].onclick = function(){
                // this指向的是当前正在操作的元素。点击第几个元素，this就表示第几个元素。
                this.style.backgroundColor = 'red';
                this.innerText = '我被点击了';
            }
        }
    </script>
    <style>
        *{
            margin:20px auto;
            padding:0;
            width:90%;
        }
        .box{
            width:200px;
            height:200px;
            margin:10px;
            float: left;
            text-align: center;
            line-height: 200px;
        }
    </style>
</body>
</html>